package com.zzl.bonds.ticket.service.common.impl;

import com.zzl.bonds.ticket.constant.status.TicketStatusEnum;
import com.zzl.bonds.ticket.service.common.TicketCommonService;
import com.zzl.bonds.ticket.untils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.Map;

/**
 * @author : spp
 * @date : 2022/2/23 20:52
 */
@Service
public class TicketCommonServiceImpl implements TicketCommonService {

    /**
     * 万为单位的数字转化成个为单位的数组
     *
     * @param params 入参map
     * @param key    参数键
     */
    @Override
    public void changeTenThousand2Number(Map<String, Object> params, String key) {
        params.computeIfPresent(key, (k, amount) -> {
            Integer financialAmountStartInt = null;
            if (amount instanceof Integer) {
                financialAmountStartInt = (Integer) amount;
            } else if (StringUtils.isNotBlank(amount + "")) {
                financialAmountStartInt = Integer.parseInt(amount + "");
            }
            if (financialAmountStartInt != null) {
                return financialAmountStartInt * 10000;
            } else {
                return null;
            }
        });
    }

    /**
     * 根据票据状态和 兑付时间 计算剩余时间
     *
     * @param expireTime     到期时间
     * @param ticketStatus 票据状态
     * @return 剩余兑付时间
     */
    @Override
    public String calculateSurplusDays(Date expireTime, Integer ticketStatus) {
       
        
        // 如果票据转台为 为 已兑付
        if (ObjectUtils.isEmpty(expireTime)) {
            return "";
        }
        String nowString = DateUtils.dateFormat(new Date(), DateUtils.DF_YYYY_MM_DD);
        //当前时间天数
        Date now = DateUtils.string2Date(nowString, DateUtils.DF_YYYY_MM_DD);
        // 已经兑付
        if (ticketStatus.equals(TicketStatusEnum.PAID.getCode())) {
            return "-";
        } else {
            // 未兑付
            long days;
            if (now.before(expireTime)) {
                // 计算剩余兑付 时间
                days = (expireTime.getTime() - now.getTime()) / (1000L * 3600L * 24L);
                return "剩余" + days + " 天";
            } else {
                // 逾期
                days = (now.getTime() - expireTime.getTime()) / (1000L * 3600L * 24L);
                return "已逾期" + days + "天";
            }
        }
    }
}
